<?php
/**
 * 这个类配置后台相关的控制器
 */
class Backend extends Controller
{

    function Backend()
	{
		parent::Controller();
        $this->load->model('book_model');
        $this->load->model('user_model');
        $this->load->model('post_model');
        $this->load->model('school_model');
        $this->load->library('session');
        $this->load->library('mypagination');
        $this->load->model('order_model');
        $this->load->model('warehouse_model');
        //$this->output->enable_profiler(TRUE); //CI的自动报告网站状态函数
        //$this->output->cache(1);   //cache the page for n minutes
	}

    function backlogin(){
      $data['title'] = "书易Bookiki-后台管理系统"; //浏览器标题栏标题
      $data['staff_status']=$this->user_model->get_staff_status(); //获取用户登录状态
      $data['error']="none";
      $this->load->view('backend/backend_login', $data);
    }

    /**
     * 登录验证函数
     */
    function login_check(){
            $input_username=$_POST['username'];
            $input_password=$_POST['passwd'];
            $role=$this->user_model->staff_login_validate($input_username,$input_password);  //调用用户模型中的函数，并将参数传递过去
            switch($role) {
               case 0: //用户不存在或密码不正确
                      $data['error']="yes";
                      $data['title']="登录书易后台管理系统";
                      $data['staff_status']=$this->user_model->get_staff_status(); //获取用户登录状态
                      $this->load->view('backend/backend_login',$data);
                      break;
               case 1:  //admin的处理函数
                    $this->session->set_userdata('staffid', $input_username);
                     $baseurl=base_url();
                     header("Location: {$baseurl}");
                     break;
               case 2:
                      break;
               case 4:
                     $this->session->set_userdata('staffid', $input_username);
                     $this->operator_main();
                     break;
            }
      }

     /**
     * 注销员工控制器，执行注销session的操作
     */
    function logout(){
        $this->load->library('session');
        $this->session->sess_destroy();
        $this->backlogin();
    }

   function operator_main(){
        $data['title'] = "书易Bookiki-订单管理系统"; //浏览器标题栏标题
        $data['staff_status']=$this->user_model->get_staff_status(); //获取用户登录状态
        if(isset($_POST['num_input']))
           $data['num_input']=$_POST['num_input'];
        else
           $data['num_input']=10;
        if(isset($_POST['inbooks'])&&isset($_POST['collector_id'])){
          $this->warehouse_model->instock($_POST['inbooks'],$_POST['collector_id']);
        }
        $data['collectors']=$this->user_model->get_collectors();
        $this->load->view('backend/into_warehouse', $data);
   }

   function order_toprocess(){
        $data['title'] = "书易Bookiki-订单管理系统"; //浏览器标题栏标题
        $data['staff_status']=$this->user_model->get_staff_status(); //获取用户登录状态
        $data['orders']=$this->order_model->get_order_toprocess();
        $this->load->view('backend/recent_order', $data);
   }

   function order_detail(){
        $data['title'] = "书易Bookiki-订单管理系统"; //浏览器标题栏标题
        $data['staff_status']=$this->user_model->get_staff_status(); //获取用户登录状态
        $order_id=$_GET['order_id'];
        $data['order_details']=$this->order_model->get_order_detail($order_id);
        $data['related_orders']=$this->order_model->get_related_order($order_id);
        $this->load->view('backend/order_detail', $data);
   }

   function order_update(){
        $data['title'] = "书易Bookiki-订单管理系统"; //浏览器标题栏标题
        $data['staff_status']=$this->user_model->get_staff_status(); //获取用户登录状态
        $order_id=$_POST['order_id'];
        $this->order_model->update_order_status($order_id,$_POST['book_ids'],$_POST['new_status']);
        $this->order_toprocess();
   }

   function search_order(){
        $data['title'] = "书易Bookiki-搜索订单"; //浏览器标题栏标题
        $data['staff_status']=$this->user_model->get_staff_status(); //获取用户登录状态
        $this->load->view('backend/search_order', $data);
   }

   function search_handle(){
       if(isset($_POST['order_id'])) $order_id=$_POST['order_id']; else $order_id=null;
       if(isset($_POST['user_id'])) $user_id=$_POST['user_id']; else $user_id=null;
       if(isset($_POST['consignee'])) $consignee=$_POST['consignee']; else $consignee=null;
       $data['orders']=$this->order_model->search_order($order_id,$user_id,$consignee);
       $data['title'] = "书易Bookiki-订单搜索结果"; //浏览器标题栏标题
       $data['staff_status']=$this->user_model->get_staff_status(); //获取用户登录状态
       $this->load->view('backend/search_result', $data);
   }




}

?>
